//#include <stdlib.h>
//#include "..\..\CommLib\Comm.h"
#include "stdafx.h"
#include "GUI.h"
#include "..\..\Global\globaldef.h"
//#include "..\Render\Render.h"
//#include "..\Common\Viewer.h"
//#include "..\Window.h"

Render::tResource* GUI::g_pButtonsMain = NULL;
Render::tResource* GUI::g_pCursor = NULL;
D3DXVECTOR2 GUI::g_CursorPosition = D3DXVECTOR2(0,0);

/*

Panel::Panel() {
	m_Position = D3DXVECTOR3(0,0,0);
	m_Dimensions = D3DXVECTOR2(0,0);
}

Panel::~Panel() {

	Release();
}

HRESULT Panel::Create() {

	m_Position = D3DXVECTOR3(0,0,0);
	return S_OK;
}

HRESULT Panel::Release() {

	return S_OK;
}

void Panel::SetPosition(D3DXVECTOR2& position) {
	
	m_Position.x = position.x;
	m_Position.y = position.y;
}

void Panel::SetSize(D3DXVECTOR2& dimensions) {
	
	m_Dimensions = dimensions;
}

void Panel::Draw() {

	UINT width, height;
	if(Render::Singleton()->GetTextureInfo(GUI::g_pButtonsMain, &width, &height)) {
		
		RECT r;
		SetRect(&r,0,0,width, 16);
		Render::Singleton()->DrawSprite(GUI::g_pButtonsMain, D3DXVECTOR2(m_Position.x,m_Position.y),D3DXVECTOR2(m_Dimensions.x / width, 1.0f), &r);

		SetRect(&r,0,16,width, height - 16);
		Render::Singleton()->DrawSprite(GUI::g_pButtonsMain, D3DXVECTOR2(m_Position.x,m_Position.y + 16),D3DXVECTOR2(m_Dimensions.x / width, m_Dimensions.y / height), &r);

		SetRect( &r, m_Position.x + 3, m_Position.y + 16 + 3, 0, 0 );
		Render::Singleton()->DrawText(Text,D3DCOLOR_RGBA(217, 235, 222,200), &r);

		SetRect( &r, m_Position.x + 4, m_Position.y + 2, 0, 0 );
		Render::Singleton()->DrawText(Title,D3DCOLOR_RGBA(60, 89, 18,255), &r);

		SetRect( &r, m_Position.x + 3, m_Position.y + 1, 0, 0 );
		Render::Singleton()->DrawText(Title, D3DCOLOR_RGBA(160, 252, 30,200), &r);
	}
}

*/
GUI::GUI() {

}

GUI::~GUI() {
	Release();
}

bool GUI::Create() {

	Render::tResource* pButtonsMain = NULL;
	Render::tResource* pCursor = NULL;

	try {

		if(!Render::Singleton()->CreateTexture("GUI\\buttons_main.jpg", &pButtonsMain))
			throw tException("Failed to create buttons texture");

		if(!Render::Singleton()->CreateTexture("GUI\\cursor.jpg", &pCursor))
			throw tException("Failed to create buttons texture");

		Render::Singleton()->ReleaseResource(&GUI::g_pButtonsMain);
		Render::Singleton()->ReleaseResource(&GUI::g_pCursor);

		GUI::g_pButtonsMain = pButtonsMain;
		GUI::g_pCursor = pCursor;
		return true;

	}catch(tException e) {

		Render::Singleton()->ReleaseResource(&pButtonsMain);
		Render::Singleton()->ReleaseResource(&pCursor);
		
		std::string message;
		tException::GetMessage(&e, message);
		return false;
	}
}

bool GUI::Release() {
	
	/*for(u_int i = 0; i < m_panels.size(); i++) {
		m_panels[i]->Release();
		delete m_panels[i];
	}
	m_panels.clear();*/

	Render::Singleton()->ReleaseResource(&GUI::g_pButtonsMain);
	Render::Singleton()->ReleaseResource(&GUI::g_pCursor);
	
	return S_OK;
}

void GUI::OnMouseEvent(const int x, const int y, const int leftButton, const int rightButton) {

	GUI::g_CursorPosition.x += x * 0.001f;
	GUI::g_CursorPosition.y += y * 0.001f;
}

void GUI::OnKeydown(UINT* pKeyboardState) {

}

float t = 0;

void GUI::Draw() {

	if(Render::Singleton()->BeginSprites()) {

		/*for(u_int i = 0; i < m_panels.size(); i++)
			m_panels[i]->Draw();*/
t+=0.05f;

		float buttonWidth = 0.1;
		float buttonHeight = 0.035;
		
		for(UINT i = 0; i < 4; i++) {

			RECT r;
			r.top = 0;
			r.bottom = 21;
			r.left = i * 64;
			r.right = (i + 1) * 64;

			Render::Singleton()->DrawSprite(GUI::g_pButtonsMain, D3DXVECTOR2(i * buttonWidth, 0.0), D3DXVECTOR2(buttonWidth,buttonHeight), &r);
		}

		
			RECT r;
			r.top = 0;
			r.bottom = 18;
			r.left = 0;
			r.right = 12;

			Render::Singleton()->DrawSprite(GUI::g_pCursor,  GUI::g_CursorPosition, D3DXVECTOR2(.03,.03), &r);


		Render::Singleton()->EndSprites();
	}
}
//
//Panel* GUI::CreatePanel() {
//
//	Panel* pPanel = new Panel();
//	if(FAILED(pPanel->Create())) {
//		delete pPanel;
//		return NULL;
//	}
//
//	m_panels.push_back(pPanel);
//	return pPanel;
//}